package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.button.MaterialButton;

import cn.lrwjz.androidguide.R;

public class BottomSheetActivity extends AppCompatActivity {
    private BottomSheetBehavior<View> standardBottomSheetBehavior;
    private MaterialButton btnToggleStandard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_sheet);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("BottomSheet 示例");
        }

        // 初始化标准 BottomSheet
        View standardBottomSheet = findViewById(R.id.standardBottomSheet);
        standardBottomSheetBehavior = BottomSheetBehavior.from(standardBottomSheet);
        
        // 设置初始状态为隐藏
        standardBottomSheetBehavior.setHideable(true);
        standardBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

        // 初始化按钮
        btnToggleStandard = findViewById(R.id.btnToggleStandard);
        updateToggleButtonText();

        // 设置标准 BottomSheet 的回调
        standardBottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                String state = "";
                switch (newState) {
                    case BottomSheetBehavior.STATE_EXPANDED:
                        state = "展开";
                        break;
                    case BottomSheetBehavior.STATE_COLLAPSED:
                        state = "折叠";
                        break;
                    case BottomSheetBehavior.STATE_DRAGGING:
                        state = "拖动中";
                        break;
                    case BottomSheetBehavior.STATE_SETTLING:
                        state = "固定中";
                        break;
                    case BottomSheetBehavior.STATE_HIDDEN:
                        state = "隐藏";
                        break;
                }
                Toast.makeText(BottomSheetActivity.this, 
                    "标准 BottomSheet 状态：" + state, 
                    Toast.LENGTH_SHORT).show();
                
                // 更新按钮文本
                updateToggleButtonText();
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                // 可以根据滑动偏移实现一些动画效果
            }
        });

        // 设置切换标准 BottomSheet 按钮点击事件
        btnToggleStandard.setOnClickListener(v -> toggleStandardBottomSheet());

        // 设置显示模态 BottomSheet 按钮点击事件
        findViewById(R.id.btnShowModal).setOnClickListener(v -> showModalBottomSheet());

        // 设置 BottomSheet 中操作按钮的点击事件
        MaterialButton btnAction = standardBottomSheet.findViewById(R.id.btnAction);
        btnAction.setOnClickListener(v -> {
            Toast.makeText(this, "点击了标准 BottomSheet 中的操作按钮", Toast.LENGTH_SHORT).show();
            standardBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
        });
    }

    private void toggleStandardBottomSheet() {
        if (standardBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
            standardBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        } else {
            standardBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
        }
    }

    private void updateToggleButtonText() {
        boolean isVisible = standardBottomSheetBehavior.getState() != BottomSheetBehavior.STATE_HIDDEN;
        btnToggleStandard.setText(isVisible ? "隐藏标准 BottomSheet" : "显示标准 BottomSheet");
    }

    private void showModalBottomSheet() {
        BottomSheetDialog dialog = new BottomSheetDialog(this);
        View view = getLayoutInflater().inflate(R.layout.bottom_sheet_content, null);
        
        MaterialButton btnAction = view.findViewById(R.id.btnAction);
        btnAction.setOnClickListener(v -> {
            Toast.makeText(this, "点击了模态 BottomSheet 中的操作按钮", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        });

        dialog.setContentView(view);
        dialog.show();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 